package com.itheima.Demo04;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

@SuppressWarnings("all")
public class Demo02 {
    public static void main(String[] args) {
        //创建一个集合 ;
        Collection<String> list = new ArrayList<>();
        //批量添加元素;
        Collections.addAll(list, "波多野结衣","abc","ABC","张晗","拜清清","abc","张梓墨");
        System.out.println("删除之前的集合"+list);
        //创建一个动态代理;
        Collection<String> list2 = (Collection<String>) Proxy.newProxyInstance(list.getClass().getClassLoader(), list.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                Object invoke = method.invoke(list, objects);
                //判断方法是不是删除方法;
                if (method.getName().equals("remove")) {
                    //遍历集合,删除元素;
                    for (int i = list.size() - 1; i >= 0; i--) {
                        method.invoke(list, objects);
                    }
                }
                return invoke;
            }
        });

        list2.remove("abc");
        System.out.println("删除之后的集合"+list);

    }
}
